The variable created within the class is called “Instance variables”. The variables created within a function are called “local variables”. When instance variables names and local variable names are the same then by default priority will be given to the local variables In the above case in order to access instance variables this keyword is required. This keyword always points to the currents class. This will be created at run time. Example of “this” keyword with instance and local variables.
Place a button
Code in GD
Code
Class EMP
{
Private int Sal = 5000;
Public void incr (int Sal)
{
This Sal = this. Sal + Sal ;
}
Public void print ()
{
Message Box. Show (“Sal = ” + Sal)
}
Code for Button 1_ click
EMP e1 = new EMP ();
E1.incr (3500); E1.print ();
Interested in mastering .NET? Learn more about ".NET Training "in this blog post.
Syntax
<Access Specifier>return type func _name (args) C#
.net allows to pass the arguments in three ways:-
Call by value
Call by reference
Call by out
When formal arguments are modified and if modification is reflected on actual arguments, then the concept is called as “call by reference” When formal arguments are modified and if modification are not reflected on actual arguments, then the concept is called as “call by value” By default variables will be passed by value. Reference is a keyword which is required to pass a variable The variables which are passing by ref must be initialized Reference keyword must be used along with actual and formal arguments.
Example on call by value and call by reference
Open windows form application project
Place a button
Code in GD
Class Test
Public void SWAP (int a, ref int b)
{
Int t = a;
A= b;
B =t;
}
}
Code for Button 1 _click
Private void button1_click ()
{
Int x = 10;
y = 20;
Test t = new Test ();
t.SWAP (x, ref y);
Message Box. Show (x + “” + y);
}
It is 99% similar to cal by ref. Out is a keyword Out allows to pass a variable without initialization also. Event if out variable is initialized, the value will not be passed. out = ref _ initialization
Example on call by out
Place a button Code in GD
Class test
{
Public void print (out int x)
{ X =10;
X= x +x;
}
}
Code for button 1_ click ()
Private void button1_ click ()
{
Int a ;
Test t = new Test ();
t.Print (out a);
Message Box. Show (a);
}
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.